package com.hlkj.log4j;

import java.io.*;
import java.util.Date;
import java.text.SimpleDateFormat;

public class LogPrintWrite {
	private PrintWriter log;
	private String logFile = "D:";
	SimpleDateFormat format;
	String datatime = "";

	/**
	 * 初始化 输出打印流
	 */
	public LogPrintWrite() {
		String file = "";
		String fullpath = "";

		file = "" + (new SimpleDateFormat("yyyy-MM-dd")).format(new Date())
				+ ".log";
		fullpath = logFile + File.separator + file;

		format = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
		try {
			log = new PrintWriter(new FileWriter(fullpath, true), true);
		} catch (IOException e) {
			System.err.println("无法打开日志文件: " + fullpath);
			log = new PrintWriter(System.err);
		}
	}

	/**
	 * 将文本信息通过日志文件输出
	 */

	public void log(String msg) {
		datatime = format.format(new Date()) + "";
		log.println(datatime + ": " + msg);
	}

	/**
	 * 将文本信息与异常写入日志文件
	 */
	public void log(Throwable e, String msg) {
		datatime = format.format(new Date()) + "";
		log.println(datatime + ": " + msg);
		e.printStackTrace(log);
	}

}
